﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using CrystalDecisions.CrystalReports.Engine;
using CUSTOMS.Modules.QLHS.Components.QLBB;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;

namespace CUSTOMS.Modules.QLHS.Controls
{
    public partial class SoTheoDoiHangHoaChuyenCuaKhau : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                DateTime today = DateTime.Today;
                DateTime lastWeek = today.Subtract(new TimeSpan(7, 0, 0, 0));
                txtTuNgay.Text = lastWeek.ToShortDateString();
                txtDenNgay.Text = today.ToShortDateString();
            }
        }

        protected void btnInThongKe_Click(object sender, EventArgs e)
        {
            Hashtable settings = this.ParentControl.Settings;

            // Report.
            ReportDocument rpt = new ReportDocument();
            string path = Server.MapPath("~/Modules/CUSTOMS.Modules.QLHS/Reports/SoTheoDoiHangHoaChuyenCuaKhau.rpt");
            rpt.Load(path);

            DateTime tungay = Convert.ToDateTime(txtTuNgay.Text);
            DateTime denngay = Convert.ToDateTime(txtDenNgay.Text);
            DataSet ds = BienBan.InThongKe(tungay, denngay, ddlLoaiHang.SelectedValue, "MKT", "%");

            if (ds.Tables[0].Rows.Count == 0)
            {
                throw new Exception("Không có số liệu");
            }

            rpt.SetDataSource(ds.Tables[0]);
            rpt.SetParameterValue("CucHaiQuan", settings["CucHaiQuan"].ToString());
            rpt.SetParameterValue("HaiQuanCaiDat", settings["TenHaiQuan"].ToString());
            rpt.SetParameterValue("TuNgay", DateTime.Parse(txtTuNgay.Text));
            rpt.SetParameterValue("DenNgay", DateTime.Parse(txtDenNgay.Text));


            Session["CUSTOMS.Modules.QLHS.Report"] = rpt;
            Response.Redirect(string.Format("~/Modules/CUSTOMS.Modules.QLHS/ViewReport.aspx?tabid={0}&mid={1}", this.TabID, this.ModuleID));
        }

        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            DateTime tungay = Convert.ToDateTime(txtTuNgay.Text);
            DateTime denngay = Convert.ToDateTime(txtDenNgay.Text);
            DataSet ds = BienBan.InThongKe(tungay, denngay, ddlLoaiHang.SelectedValue, ddlTruongHop.SelectedValue, "%");

            if (ds.Tables[0].Rows.Count == 0)
            {
                throw new Exception("Không có số liệu");
            }
            gvThongKe.DataSource = ds;
            gvThongKe.DataBind();
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            gvThongKe.PageIndex = ddl.SelectedIndex;
            gvThongKe.DataBind();
        }

        protected void gvThongKe_DataBound(object sender, EventArgs e)
        {
            if (gvThongKe.Rows.Count > 0)
            {
                // Retrieve the pager row.
                GridViewRow bottomPagerRow = gvThongKe.BottomPagerRow;
                GridViewRow topPagerRow = gvThongKe.TopPagerRow;

                // Retrieve the DropDownList and Label controls from the row.
                DropDownList topPageList = (DropDownList)topPagerRow.Cells[0].FindControl("PageDropDownList");
                DropDownList bottomPageList = (DropDownList)bottomPagerRow.Cells[0].FindControl("PageDropDownList");
                Label topPageLabel = (Label)topPagerRow.Cells[0].FindControl("PageLabel");
                Label bottomPageLabel = (Label)bottomPagerRow.Cells[0].FindControl("PageLabel");

                if (topPageList != null && bottomPageList != null)
                {
                    for (int i = 0; i < gvThongKe.PageCount; i++)
                    {
                        int pageNumber = i + 1;
                        ListItem item = new ListItem(pageNumber.ToString());
                        if (i == gvThongKe.PageIndex)
                        {
                            item.Selected = true;
                        }

                        topPageList.Items.Add(item);
                        bottomPageList.Items.Add(item);
                    }
                }

                if (topPageLabel != null && bottomPageLabel != null)
                {
                    topPageLabel.Text = bottomPageLabel.Text = " / " + gvThongKe.PageCount;
                }
            }
        }

    }
}